package org.atlantis.datasink.event;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class EventNamedThreadFactory implements ThreadFactory {

	private static final String THREAD_NAME_PREFIX = "datasink:event:";
	private static final String FREE_THREAD_NAME = THREAD_NAME_PREFIX + "free";
	private static final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();

	@Override
	public Thread newThread(Runnable r) {
		Thread t = defaultThreadFactory.newThread(r);
		t.setName(FREE_THREAD_NAME);
		return t;
	}

	public static void modifyName(String newName) {
		Thread.currentThread().setName(THREAD_NAME_PREFIX + newName);
	}

	public static void restoreName() {
		Thread.currentThread().setName(FREE_THREAD_NAME);
	}
}
